package com.xb.cloud.shopping.client;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.xb.cloud.common.JsonUtils;
import com.xb.cloud.common.util.StringUtils;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.order.service.client.bo.SellOrderOperationBo;
import com.xb.cloud.shopping.client.vo.CheckOutInputVo;
import com.xb.cloud.shopping.model.ShoppingCartItem;
import com.xb.cloud.shopping.service.ShoppingOrderApiService;

@Component
public class ShoppingOrderAdapter extends Adapter {

	private final Logger logger = LoggerFactory.getLogger(ShoppingOrderAdapter.class);

	@Resource
	ShoppingOrderApiService shoppingOrderApiService;

	/**
	 * 
	 * <p>
	 * Title: execute
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param urlPath
	 * @param isLogined
	 * @param bizInfo
	 * @param context
	 * @return
	 * @see com.xb.cloud.gateway.client.Adapter#execute(java.lang.String,
	 *      java.lang.Boolean, java.util.Map,
	 *      com.xb.cloud.gateway.client.AdapterContext)
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		long time = System.currentTimeMillis();

		RtnInfo rtnInfo = null;
		long companyId = context.getCompanyId();
		long currentUserId = context.getCurrentUserId();

		if ("/h5/shopping/order/createAccountPage".equalsIgnoreCase(urlPath)) {
			CheckOutInputVo cartCheckOutVo = new CheckOutInputVo();
			cartCheckOutVo.setCompanyId(companyId);
			cartCheckOutVo.setCurrentUserId(currentUserId);
			String items = bizInfo.get("items");
			List<ShoppingCartItem> cartItemList = JsonUtils.toObjectArray(items, ShoppingCartItem.class);
			cartCheckOutVo.setItemList(cartItemList);
			return shoppingOrderApiService.createAccountPage(cartCheckOutVo);
		}

		if ("/h5/shopping/order/generateOrder".equalsIgnoreCase(urlPath)) {
			CheckOutInputVo cartCheckOutVo = new CheckOutInputVo();
			cartCheckOutVo.setCompanyId(companyId);
			cartCheckOutVo.setCurrentUserId(currentUserId);
			
			String items = bizInfo.get("items");
			String addressId = bizInfo.get("addressId");
			String receiverName = bizInfo.get("receiverName");
			String receiverPhone = bizInfo.get("receiverPhone");
			String remark = bizInfo.get("remark");
			
			List<ShoppingCartItem> cartItemList = JsonUtils.toObjectArray(items, ShoppingCartItem.class);
			cartCheckOutVo.setItemList(cartItemList);
			cartCheckOutVo.setReceiverName(receiverName);
			cartCheckOutVo.setReceiverPhone(receiverPhone);
			cartCheckOutVo.setRemark(remark);
			if (StringUtils.isNotBlank(addressId)) {
				cartCheckOutVo.setAddressId(Long.valueOf(addressId));
			}
			
			return shoppingOrderApiService.generateOrder(cartCheckOutVo);
		}

		if ("/h5/shopping/order/cancel".equalsIgnoreCase(urlPath)) {
			SellOrderOperationBo operationBo = new SellOrderOperationBo();
			operationBo.setCompanyId(context.getCompanyId());
			operationBo.setEndUserId(context.getCurrentUserId());
			operationBo.setEndUserName(context.getCurrentUserName());
			operationBo.setOrderId(Long.valueOf(bizInfo.get("orderId")));
			return shoppingOrderApiService.cancelOrder(operationBo);

		}
		if ("/h5/shopping/order/delete".equalsIgnoreCase(urlPath)) {
			SellOrderOperationBo operationBo = new SellOrderOperationBo();
			operationBo.setCompanyId(context.getCompanyId());
			operationBo.setEndUserId(context.getCurrentUserId());
			operationBo.setEndUserName(context.getCurrentUserName());
			operationBo.setOrderId(Long.valueOf(bizInfo.get("orderId")));
			return shoppingOrderApiService.deleteOrder(operationBo);

		}

		logger.debug("执行时间：{}", System.currentTimeMillis() - time);
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/shopping/order/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}
}